﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BZV.Foundation;
using BZV.Foundation.Business;
using BZV.Core.Net;
using System.Threading;
using System.IO;


namespace LotteryHelper
{
    public partial class MainForm : Form
    {
        private delegate void invokeDelegate(object sender, EventArgs e);
        QueryCode code;
        DataPool m_data;
        int nSelIndex;
        UserCount m_UserCount = new UserCount();
        
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            ListLotteryControl m_CtrlListLottery = new ListLotteryControl();
            m_CtrlListLottery.InitializeUI("255401", 120);
            m_data = m_CtrlListLottery.m_DataPool;
            m_CtrlListLottery.Dispose();
            Analyzer a = new Analyzer();
            a.SetData(m_data.Lotterys);
            a.CalculateCombinThreeToSixMatrix();

            LotteryComPerdictor p = new LotteryComPerdictor();
            p.SetData(m_data.Lotterys);
            p.ForecastCombinSix(0);
            p.ForecastNum(0, 7);

            m_OldLotteryCtrl.Name = "旧时时彩";
            m_OldLotteryCtrl.InitializeUI("255401", 120);
//             m_NewLotteryCtrl.Name = "新时时彩";
//             m_NewLotteryCtrl.InitializeUI("258001", 84);

            InitialTestPage(m_OldLotteryCtrl.m_DataPool.Lotterys);

            // 设置标题
            m_lsvTestForcast.Columns.Add("项目", 38, HorizontalAlignment.Center);
            m_lsvTestForcast.Columns.Add("组态", 38, HorizontalAlignment.Center);
            for (int i = 0; i < 10; i++)
            {
                m_lsvTestForcast.Columns.Add(i.ToString(), 27, HorizontalAlignment.Center);
            }
            m_lsvTestForcast.View = View.Details;
        }

        public void StartThread(DataPool data)
        {
            this.Invoke((EventHandler)delegate
            {
                
            });

            // 20个数字的二阶
            new Thread((ThreadStart)delegate
                {
                    Analyzer a = new Analyzer();
                    a.SetData(data.Lotterys);
                    List<int[,]> vs1 = new List<int[,]>();
                    for (int i = 0; i < 10; i++)
                    {
                        int[,] val = a.CalulateNumMatrix(i);
                        vs1.Add(val);
                    }

                    // 写入文件
                    SerializeHelper.SerializeToBinary("d:\\2.dat", vs1);
                }).Start();


            // 10个数字的二阶
            new Thread((ThreadStart)delegate
            {
                Analyzer a = new Analyzer();
                a.SetData(data.Lotterys);
                List<int[,]> vs2 = new List<int[,]>();
                for (int i = 0; i < 10; i++)
                {
                    int[,] val = a.CalulateNumMatrix(i, 10);
                    vs2.Add(val);
                }

                // 写入文件
                SerializeHelper.SerializeToBinary("d:\\3.dat", vs2);
            }).Start();

            // 10个数字的三阶
            new Thread((ThreadStart)delegate
            {
                Analyzer a = new Analyzer();
                a.SetData(data.Lotterys);
                List<int[,,]> vs3 = new List<int[,,]>();
                for (int i = 0; i < 10; i++)
                {
                    int[,,] val = a.CalulateNumMatrix3(i, 10);
                    vs3.Add(val);
                }

                // 写入文件
                SerializeHelper.SerializeToBinary("d:\\4.dat", vs3);
            }).Start();

            // 连续数字个数到不出现数字个数
            new Thread((ThreadStart)delegate
            {
                Analyzer a = new Analyzer();
                a.SetData(data.Lotterys);
                List<int[,]> vs4 = new List<int[,]>();
                for (int i = 0; i < 10; i++)
                {
                    int[,] val = a.CalulateContinueToUnMatrix(i);
                    vs4.Add(val);
                }

                // 写入文件
                SerializeHelper.SerializeToBinary("d:\\5.dat", vs4);
            }).Start();

            // 20个组六到20个组六 三阶
            new Thread((ThreadStart)delegate
            {
                Analyzer a = new Analyzer();
                a.SetData(data.Lotterys);
                int[, ,] val5 = a.CalculateCombinSixToSixMatrix3();

                // 写入文件
                SerializeHelper.SerializeToBinary("d:\\6.dat", val5);
            }).Start();

            // 20个组六到20个组六 二阶
            new Thread((ThreadStart)delegate
            {
                Analyzer a = new Analyzer();
                a.SetData(data.Lotterys);
                int[,] val6 = a.CalculateCombinSixToSixMatrix();

                // 写入文件
                SerializeHelper.SerializeToBinary("d:\\7.dat", val6);
            }).Start();

            // 连续组三后组六个数
            new Thread((ThreadStart)delegate
            {
                Analyzer a = new Analyzer();
                a.SetData(data.Lotterys);
                int[,] val7 = a.CalculateCombinThreeToSixMatrix();

                // 写入文件
                SerializeHelper.SerializeToBinary("d:\\8.dat", val7);
            }).Start();

            // 连续组六后组三个数
            new Thread((ThreadStart)delegate
            {
                Analyzer a = new Analyzer();
                a.SetData(data.Lotterys);
                int[,] val8 = a.CalculateCombinSixToThreeMatrix();

                // 写入文件
                SerializeHelper.SerializeToBinary("d:\\9.dat", val8);
            }).Start();

            // 20个组三到20个组三 三阶
            new Thread((ThreadStart)delegate
            {
                Analyzer a = new Analyzer();
                a.SetData(data.Lotterys);
                int[, ,] val9 = a.CalculateCombinThreeToThreeMatrix3();

                // 写入文件
                SerializeHelper.SerializeToBinary("d:\\10.dat", val9);
            }).Start();

            // 20个组三到20个组三 二阶
            new Thread((ThreadStart)delegate
            {
                Analyzer a = new Analyzer();
                a.SetData(data.Lotterys);
                int[,] val10 = a.CalculateCombinThreeToThreeMatrix();

                // 写入文件
                SerializeHelper.SerializeToBinary("d:\\11.dat", val10);
            }).Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LotteryComPerdictor per = new LotteryComPerdictor();
            per.SetData(m_data.Lotterys);
            ThreeNumerialParam pa = per.GetNumerialParam(10, 100);
            ThreeNumerialParam pa1 = per.GetNumerialParam(0, 10);

            int[] sortpa = pa.Sort();
            int[] sortpa1 = pa1.Sort();
        }

        private void ReadLottery(string strID, string strFileName, int nCount = 120)
        {
            // 开始获取列表
            new Thread((ThreadStart)delegate
            {
                // 保存
                string strPre = "0";
                if (nCount != 120)
                {
                    strPre = "1";
                }

                // 获取数据
                List<Lottery> lots = DataPool.GetAtCount(strID, 150000, nCount);
                using (StreamWriter sw = new StreamWriter(strFileName))
                {
                    foreach (Lottery lottery in lots)
                    {
                        string str = "20" + lottery.Issue + "\t"
                                    + lottery.Code[0] + "\t"
                                    + lottery.Code[1] + "\t"
                                    + lottery.Code[2] + "\t"
                                    + lottery.Code[3] + "\t"
                                    + lottery.Code[4] + "\t"
                                    + strPre;
                        sw.WriteLine(str);
                    }

                }
            }
             ).Start();
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //m_CtrlListLottery.Dispose();
            //m_NewLottery.Dispose();
        }

        private void MainForm_Activated(object sender, EventArgs e)
        {
            
        }

        private void MainForm_MouseMove(object sender, MouseEventArgs e)
        {
            //Activate();
            //Focus();
        }

        private void ShowForecast(int nIndex)
        {
            // 组态预测
            LotteryComPerdictor p = new LotteryComPerdictor();
            p.SetData(m_data.Lotterys);
            int[] arr = p.ForecastCombinSix(nIndex);

            string str = "组态预测\n";
            Analyzer an = new Analyzer();
            an.SetData(m_data.Lotterys);
            object ddd = an.CalculateBestBefore80();
            UintParam param = an.GetUintIndex(nIndex, 15);
            int nSix = param.CombinationCount[CombinationState.EN_COM_SIX];

            UintParam lastParam = an.GetUintIndex(nIndex + 20 + 15);
            UintParam curParam = an.GetUintIndex(nIndex + 15);
            str += "1节：" + lastParam.CombinationCount[CombinationState.EN_COM_SIX].ToString() + "\n";
            str += "2节：" + curParam.CombinationCount[CombinationState.EN_COM_SIX].ToString() + "\n";
            str += "当前15中：" + param.CombinationCount[CombinationState.EN_COM_SIX].ToString() + "\n";
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                if (arr[i] <= 1 && i < 10)
                {
                    continue;
                }
                str += i.ToString() + ":" + arr[i].ToString() + "\t" + (i - nSix).ToString() + "\n";
            }

            MessageBox.Show(str);

            
        }

        private void ShowForecastNum(int nIndex, int nNo)
        {
            // 数字预测
            LotteryComPerdictor p = new LotteryComPerdictor();
            p.SetData(m_data.Lotterys);
            int[] NumArr = p.ForecastNum(nIndex, nNo);

            string str = "数字预测\n";
            Analyzer an = new Analyzer();
            an.SetData(m_data.Lotterys);

            // 当前15个数字的个数
            int nCurCount = an.GetNumCount(nIndex, 15)[nNo];
            int nLastCount = an.GetNumCount(nIndex + 20 + 15)[nNo];
            int nNextCount = an.GetNumCount(nIndex + 15)[nNo];

            // 显示的内容
            str += "第一阶:" + nLastCount.ToString() + "\n";
            str += "第二阶:" + nNextCount.ToString() + "\n";
            str += "当前:" + nCurCount.ToString() + "\n";
            for (int i = 0; i < NumArr.GetLength(0); i++)
            {
                if (NumArr[i] <= 0)
                {
                    continue;
                }
                str += i.ToString() + ":" + NumArr[i] + "\t" + (i - nCurCount).ToString() + "\n";
            }

            MessageBox.Show(str);
            
        }

        /// <summary>
        /// 在listview中显示当前的组态和数字的预测情况
        /// </summary>
        /// <param name="nIndex"></param>
        /// <param name="lsv"></param>
        private void ShowAllForcast(int nIndex, ListView lsv)
        {
            LotteryComPerdictor p = new LotteryComPerdictor();
            p.SetData(m_data.Lotterys);

            // 组态预测
            int[] arrCombinForcast20 = p.ForecastCombinSix(nIndex);
            Analyzer an = new Analyzer();
            an.SetData(m_data.Lotterys);
            object ddd = an.CalculateBestBefore80();
            UintParam param = an.GetUintIndex(nIndex, 15);                      // 当前的15个组态情况
            int nCombinCurSix = param.CombinationCount[CombinationState.EN_COM_SIX];
            UintParam lastParam = an.GetUintIndex(nIndex + 20 + 15);            // 第一阶20个的组态情况
            int nCombinLastSix = lastParam.CombinationCount[CombinationState.EN_COM_SIX];
            UintParam nextParam = an.GetUintIndex(nIndex + 15);                  // 第二阶20个的组态情况
            int nCombinNextSix = nextParam.CombinationCount[CombinationState.EN_COM_SIX];

            // 数字预测
            int[] firstNumParams = new int[10];
            int[] secNumParams = new int[10];
            int[] curNumParams = new int[10];
            int[,] numForcastParams = new int[10,21];
            for (int i = 0; i < 10; i++)
            {
                int[] arrNumForcast20 = p.ForecastNum(nIndex, i);
                for (int j = 0; j < numForcastParams.GetLength(1); j++)
                {
                    numForcastParams[i,j] = arrNumForcast20[j];
                }

                // 当前15个数字的个数
                int nCurCount = an.GetNumCount(nIndex, 15)[i];
                int nLastCount = an.GetNumCount(nIndex + 20 + 15)[i];
                int nNextCount = an.GetNumCount(nIndex + 15)[i];
                firstNumParams[i] = nLastCount;
                secNumParams[i] = nNextCount;
                curNumParams[i] = nCurCount;
            }

            // 开始填充列表(3个特殊的，21个数值)
            lsv.BeginUpdate();
            lsv.Items.Clear();
            for (int i = 0; i < 24; i++)
            {
                ListViewItem item = new ListViewItem();
                

                for (int n = 0; n < 11; n++)
                {
                    if (i == 0)                         // 第一行
                    {
                        item.Text = "一阶";
                        if (n == 0)                     // 第一列
                        {
                            item.SubItems.Add(nCombinLastSix.ToString());
                        }
                        else
                        {
                            item.SubItems.Add(firstNumParams[n - 1].ToString());
                        }
                    }
                    else if (i == 1)                    // 第二行
                    {
                        item.Text = "二阶";
                        if (n == 0)                     // 第一列
                        {
                            item.SubItems.Add(nCombinNextSix.ToString());
                        }
                        else
                        {
                            item.SubItems.Add(secNumParams[n - 1].ToString());
                        }
                    }
                    else if (i == 2)                    // 第三行
                    {
                        item.Text = "当前";
                        if (n == 0)                     // 第一列
                        {
                            item.SubItems.Add(nCombinCurSix.ToString());
                        }
                        else
                        {
                            item.SubItems.Add(curNumParams[n - 1].ToString());
                        }
                    }
                    else
                    {
                        item.Text = (i - 3).ToString();

                        if (n == 0)                     // 第一列
                        {
                            item.SubItems.Add(arrCombinForcast20[i - 3].ToString());
                        }
                        else
                        {
                            item.SubItems.Add(numForcastParams[n - 1, i - 3].ToString());
                        }
                    }
                }

                

                // 添加到列表中
                lsv.Items.Add(item);
            }

            lsv.EndUpdate();

        }

        /// <summary>
        /// 指定位置和个数的刷新
        /// </summary>
        /// <param name="nIndex"></param>
        /// <param name="nCount"></param>
        public void UpdateTestList(int nIndex, int nCount, List<Lottery> lotterys)
        {
            // 清除以前的
            m_ListTestLottory.BeginUpdate();
            m_ListTestLottory.Items.Clear();
            for (int i = nIndex; i < nIndex + nCount; i++)
            {
                Lottery lottery = lotterys[i];

                // 创建一行
                ListViewItem item = new ListViewItem();

                // 添加期号
                item.Text = lottery.Issue;

                // 添加开奖号
                ListViewItem.ListViewSubItem subItem = item.SubItems.Add(lottery.Code);

                // 添加组态
                string strComState = "";
                ListViewItem.ListViewSubItem subItemCom = item.SubItems.Add("");
                switch (lottery.Param.comState)
                {
                    case CombinationState.EN_COM_ALL:           // 豹子 
                        {
                            strComState = "豹子";
                            subItemCom.BackColor = Color.Blue;
                            break;
                        }
                    case CombinationState.EN_COM_SIX:
                        {
                            strComState = "组六";
                            break;
                        }
                    case CombinationState.EN_COM_THREE:
                        {
                            strComState = "组三";
                            subItemCom.BackColor = Color.Red;
                            break;
                        }
                }
                subItemCom.Text = strComState;
                item.BackColor = subItemCom.BackColor;

                // 加入到列表中
                m_ListTestLottory.Items.Add(item);
            }

            m_ListTestLottory.EndUpdate();
        }

        /// <summary>
        /// 初始化模拟界面
        /// </summary>
        public void InitialTestPage(List<Lottery> lotterys)
        {
            // 初始化列表
            m_ListTestLottory.Columns.Add("期号", 90);
            m_ListTestLottory.Columns.Add("开奖号", 80);
            m_ListTestLottory.Columns.Add("组态");
            m_ListTestLottory.View = View.Details;

            // 设置一个随机的位置
            int rndPos = CalcHelper.CreateRandNum(0, lotterys.Count - 20);
            nSelIndex = rndPos;
            UpdateTestList(rndPos, 20, lotterys);
        }

        /// <summary>
        /// 选中所有的数字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_SelectedAllNum_Click(object sender, EventArgs e)
        {
            m_NumChoose.SelectedAll();
        }

        /// <summary>
        /// 清除所有的数字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_DesSelectedAllNum_Click(object sender, EventArgs e)
        {
            m_NumChoose.DeSelectedAll();
        }

        /// <summary>
        /// 开奖
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_btnTestNext_Click(object sender, EventArgs e)
        {
            // 刷新列表
            nSelIndex--;
            if (nSelIndex <= 20)
            {
                nSelIndex = CalcHelper.CreateRandNum(0, m_data.Lotterys.Count - 20);
            }
            UpdateTestList(nSelIndex, 20, m_data.Lotterys);

            // 得到输入信息
            CombinationState state;
            if (m_rdSix.Checked)
            {
                state = CombinationState.EN_COM_SIX;
            }
            else
            {
                state = CombinationState.EN_COM_THREE;
            }

            // 得到新的注
            Lottery newLottery = m_data.Lotterys[nSelIndex];
            m_UserCount.SetBouns(Convert.ToDouble(m_txtSixBonus.Text), Convert.ToDouble(m_txtThreeBonus.Text));

            // 开奖
            int nTimes = Convert.ToInt32(m_txtTimes.Text);
            m_UserCount.SetBet(state, m_NumChoose.SelectedNum, nTimes, newLottery.Code);

            // 得到信息
            m_lblCorrectPer.Text = "正确率" + m_UserCount.dCorrectPercent.ToString();
            m_lblCorrectTimes.Text = "正确数" + m_UserCount.nCorrectTimes.ToString();
            m_lblTotalTimes.Text = "总次数" + m_UserCount.nTotalTimes.ToString();
            m_lblMaxCost.Text = "最大注" + m_UserCount.dMaxCost.ToString();
            m_lblTotalmoney.Text = "总钱" + m_UserCount.dTotalMoney.ToString();

            new Thread((ThreadStart)delegate
            {
                this.Invoke((EventHandler)delegate
                    {
                        //ShowAllForcast(nSelIndex, m_lsvTestForcast);
                    });
            }).Start();
            
        }

        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_btnTestClear_Click(object sender, EventArgs e)
        {
            m_UserCount.InitialData();
            nSelIndex = CalcHelper.CreateRandNum(0, m_data.Lotterys.Count - 20);

            // 得到信息
            m_lblCorrectPer.Text = "正确率" + m_UserCount.dCorrectPercent.ToString();
            m_lblCorrectTimes.Text = "正确数" + m_UserCount.nCorrectTimes.ToString();
            m_lblTotalTimes.Text = "总数" + m_UserCount.nTotalTimes.ToString();
            m_lblMaxCost.Text = "最大注" + m_UserCount.dMaxCost.ToString();
            m_lblTotalmoney.Text = "总钱" + m_UserCount.dTotalMoney.ToString();

            UpdateTestList(nSelIndex, 20, m_data.Lotterys);
        }

        /// <summary>
        /// 预测
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_btnTestForecast_Click(object sender, EventArgs e)
        {

            ShowAllForcast(nSelIndex, m_lsvTestForcast);
            // 数字
            int nNo = Convert.ToInt32(m_txtTestForecast.Text);
            if (nNo >= 0)
            {
                //ShowForecastNum(nSelIndex, nNo);
            }
            else
            {
                // 组态
                //ShowForecast(nSelIndex);
            }
        }

        /// <summary>
        /// 预测当前的情况
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_btnForecast_Click(object sender, EventArgs e)
        {
            

            // 数字
            int nNo = Convert.ToInt32(m_txtForecast.Text);
            if (nNo >= 0)
            {
                ShowForecastNum(0, nNo);
            }
            else
            {
                ShowForecast(0);
            }
        }

        
    }
}
